GOAL: Use R to replicate this set of interactive tables and charts from Our World in Data on life satisfaction across countries.
The tabs below present statistics on the Cantril Life Ladder question: “Please imagine a ladder, with steps numbered from 0 at the bottom to 10 at the top. The top of the ladder represents the best possible life for you and the bottom of the ladder represents the worst possible life for you. On which step of the ladder would you say you personally feel you stand at this time?”
TABLE TO BE CREATED HERE
# In this tab, you will create a table similar to the one on the Our World in Data page.
# The table should have the following columns:
# Country, `2011`, `2022`, Absolute change, and Relative change.
# You should:
# 1. Use `select` to drop the Code column from the `cantril` data as it is not needed in the table.
# 2. Filter the `cantril` data frame to just the years 2011 and 2022.
# 3. Use the `pivot_wider` function to create the columns for 2011 and 2022.
# (You may need backticks around column names with spaces or which start with numbers.)
# 4. Use the `mutate` function to calculate the Absolute change and Relative change.
# 5. Also with `mutate`, you should round your numbers to appropriate decimal places.
# 6. Then use scales::percent to format the Relative change as a percentage.
# 7. Use the `reactable` function to create and output the table.
CHART TO BE CREATED HERE
# In this tab, create a simple line chart of the Cantril score over time for a few countries,
# similar to the one on the Our World in Data page.
# Note that your chart will not have the ability to filter countries like the one on the Our World in Data page.
# You should:
# 1. Filter the cantril data to just 4 to 8 countries you want to highlight
# 2. Use ggplot to create a line chart of the Cantril score over time, with different colors for each country
# 3. Use geom_line to create the lines
# 4. Use scale_y_continuous to set the y-axis limits to 0 and 10.
# 5. Use labs to create neat labels for the chart
# 6. Use theme_minimal to set the theme.
# 7. Finally, use ggplotly to convert the ggplot chart to a plotly chart
One problem with the simple chart on the previous tab is that the
countries are fixed. It would be better if the user could select the
countries they want to see. This is not possible with
plotly alone, but with the crosstalk package
and some custom JavaScript, we can achieve this, as demonstrated
below.
This is simply a demonstration for you, you do not need to work on this for your assignment. You may want to come back to this when you need such functionality in your own projects.
For more information on how we achieved this, see this Stack Overflow question.
Data source: World Happiness Report